package com.hz.ruoyifarmbase.farm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hz.ruoyifarmbase.baseinfo.domain.AgrBasePlots;
import com.hz.ruoyifarmbase.baseinfo.mapper.AgrBasePlotsMapper;
import com.hz.ruoyifarmbase.farm.domain.AgrBaseBatch;
import com.hz.ruoyifarmbase.farm.domain.AgrBaseProcess;
import com.hz.ruoyifarmbase.farm.domain.vo.AgrProcessOperateVo;
import com.hz.ruoyifarmbase.farm.mapper.AgrBaseBatchMapper;
import com.hz.ruoyifarmbase.farm.mapper.AgrBaseOperateMapper;
import com.hz.ruoyifarmbase.farm.mapper.AgrBaseProcessMapper;
import com.hz.ruoyifarmbase.farm.service.AgrBaseProcessService;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;

/**
 * @author 高铭月
 * @date 2025/3/5 16:22
 * @description:
 */
@Service
public class AgrBaseProcessServiceImpl
        extends ServiceImpl<AgrBaseProcessMapper, AgrBaseProcess>
        implements AgrBaseProcessService {


    @Autowired
    private AgrBaseProcessMapper agrBaseProcessMapper;

    @Autowired
    private AgrBaseBatchMapper agrBaseBatchMapper;

    @Autowired
    private AgrBasePlotsMapper agrBasePlotsMapper;
    @Override
    public List<AgrBaseProcess> getProcessByTypeId(Long typeId) {
        // 参数校验
        if (typeId == null) {
            throw new IllegalArgumentException("种质类型ID不能为空");
        }
        // 构建查询条件
        LambdaQueryWrapper<AgrBaseProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgrBaseProcess::getTypeId, typeId)
                .orderByAsc(AgrBaseProcess::getProcessSort);
        // 执行查询
        return this.list(queryWrapper);
    }

    /**
     * 根据种质类型ID查询流程信息
     * @param typeId
     * @return
     */
    @Override
    public List<AgrProcessOperateVo> getProcessByTypeIds(Long typeId) {
        return agrBaseProcessMapper.getProcessByTypeIds(typeId);
    }

    /**
     * 根据流程ID查询流程信息
     * @param processId
     * @return
     */
    @Override
    public List<AgrBaseProcess> getProcessByProcessId(Long processId) {
        LambdaQueryWrapper<AgrBaseProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgrBaseProcess::getProcessId, processId);
        return this.list(queryWrapper);
    }

    /**
     * 当流程任务完成时，更新批次状态和地块状态
     * @param agrProcessOperateVo 作物种植操作
     * @return
     */
    @Override
    @Transactional
    public int update(AgrProcessOperateVo agrProcessOperateVo) {
        AgrBasePlots agrBasePlots = new AgrBasePlots();
        agrBasePlots.setPlotsId(agrProcessOperateVo.getPlotsId());
        agrBasePlots.setPlotsStatus(0l);
        agrBasePlotsMapper.updateAgrBasePlots(agrBasePlots);
        AgrBaseBatch agrBaseBatch = new AgrBaseBatch();
        agrBaseBatch.setBatchId(agrProcessOperateVo.getBatchId());
        agrBaseBatch.setStatus(2);
        agrBaseBatchMapper.updateById(agrBaseBatch);
        return 1;
    }

}
